﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace hw2
{
    internal interface Graph
    {
        double area();
        bool isLegal();
    }

    internal class Rectangle : Graph
    {
        int length;
        int width;
        public Rectangle(int length, int width)
        {
            this.length = length;
            this.width = width;
        }
        public double area()
        {
            return length * width;
        }

        public bool isLegal()
        {
            if (length <= 0 || width <= 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }

    internal class Circle : Graph
    {
        int radius;
        public Circle(int r)
        {
            this.radius = r;
        }

        public double area()
        {
            return Math.PI * radius * radius;
        }

        public bool isLegal()
        {
            if (radius <= 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }

    internal class Triangle : Graph
    {
        int edge1;
        int edge2;
        int edge3;
        public Triangle(int e1,int e2,int e3)
        {
            this.edge1 = e1;
            this.edge2 = e2;
            this.edge3 = e3;
        }

        public double area()
        {
            //海伦公式
            double p = (edge1 + edge2 + edge3) / 2;
            double s = Math.Sqrt(p * (p - edge1) * (p - edge2) * (p - edge3));
            return s;
        }

        public bool isLegal()
        {
            if (edge1 <= 0 || edge2 <= 0 || edge3 <= 0)
            {
                return false;
            }
            else if(edge1 + edge2 > edge3&&
                    edge1 + edge3 > edge2&&
                    edge2 + edge3 > edge1)
            {
                return true;
            }
            else
            {
                return true;
            }
        }
    }

    internal class Test
    {
        public static void Main()
        {
            List<Rectangle> rectangles = new List<Rectangle>();
            //4用来测试isLegal()
            Rectangle rectangle1 = new Rectangle(4,3);
            Rectangle rectangle2 = new Rectangle(7,4);
            Rectangle rectangle3 = new Rectangle(2,6);
            Rectangle rectangle4 = new Rectangle(2,-2);
            rectangles.Add(rectangle1);
            rectangles.Add(rectangle2);
            rectangles.Add(rectangle3);
            rectangles.Add(rectangle4);
            double s1 = 0.0;

            List<Circle> circles = new List<Circle>();
            Circle circle1 = new Circle(3);
            Circle circle2 = new Circle(2);
            Circle circle3 = new Circle(5);
            Circle circle4 = new Circle(8);
            circles.Add(circle1);
            circles.Add(circle2);
            circles.Add(circle3);
            circles.Add(circle4);
            double s2 = 0.0;

            //1用来测试isLegal()
            List<Triangle> triangles = new List<Triangle>();
            Triangle triangle1 = new Triangle(2, 2, 4);
            Triangle triangle2 = new Triangle(5, 3, 4);
            Triangle triangle3 = new Triangle(5, 2, 4);
            Triangle triangle4 = new Triangle(3, 5, 7);
            triangles.Add(triangle1);
            triangles.Add(triangle2);
            triangles.Add(triangle3);
            triangles.Add(triangle4);
            double s3 = 0.0;

            foreach(Rectangle rectangle in rectangles)
            {
                if (rectangle.isLegal())
                {
                    s1 += rectangle.area();
                }
            }
            foreach(Circle circle in circles)
            {
                if (circle.isLegal())
                {
                    s2 += circle.area();
                }
            }
            foreach(Triangle triangle in triangles)
            {
                if (triangle.isLegal())
                {
                    s3 += triangle.area();
                }
            }


            Console.Write("这是个图形的面积和是:" + s1 + s2 + s3);
            
        }
    }
}
